$(document).ready(function() {
	var idUpdate = "";
	var idDelete = "";
	$('#edit').on('hidden.bs.modal', function() {
		$('[name=title]').val("");
		$('[name=type]').val("");
		$('[name=duration]').val("");
		$('[name=url_image]').val("");
		$('[name=url_video]').val("");
		$('[name=description]').val("");
	});

	$(".editBtn").click(function() {
		var idMovie = $(this).data("movie");
		idUpdate = idMovie;
		$.ajax({
			type : 'POST',
			url : '/cine-mtl/adminEdit',
			data : 'idMovie=' + idMovie,
			success : function(data) {
				var movie = $.parseJSON(data);
				$('[name=title]').val(movie.title);
				$('[name=type]').val(movie.type);
				$('[name=duration]').val(movie.duration);
				$('[name=url_image]').val(movie.urlImage);
				$('[name=url_video]').val(movie.urlVideo);
				$('[name=description]').val(movie.description);

			},
			error : function(data) {
				console.log('not logout');
			}
		});
	});
	
	$("#update").click(function() {
		var movie = {};
		movie.id = idUpdate;
		movie.title = $('[name=title]').val();
		movie.type = $('[name=type]').val();
		movie.duration = $('[name=duration]').val();
		movie.urlImage = $('[name=url_image]').val();
		movie.urlVideo = $('[name=url_video]').val();
		movie.description = $('[name=description]').val();
		$.ajax({
			type : 'POST',
			url : '/cine-mtl/adminUpdate',
			data : "movie=" + JSON.stringify(movie),

			success : function(data) {
				$("#edit").modal("toggle");
				location.reload();
				
			},
			error : function(data) {
				console.log('not logout');
			}
		});
	});
	
	$(".deleteBtn").click(function() {

		 idDelete = $(this).data("movie");
		
	});

	$("#deleteBtn").click(function() {

		
		$.ajax({
			type : 'POST',
			url : '/cine-mtl/adminDelete',
			data : 'idMovie=' + idDelete,
			success : function(data) {
				$("#delete").modal("toggle");
				$("#row"+idDelete).remove();
			},
			error : function(data) {
				console.log('not logout');
			}
		});
	});
		
	
		$("#addBtn").click(function() {
			
			var movie = {};
			movie.title = $('[name=titleAdd]').val();
			movie.type = $('[name=typeAdd]').val();
			movie.duration = $('[name=durationAdd]').val();
			movie.urlImage = $('[name=url_imageAdd]').val();
			movie.urlVideo = $('[name=url_videoAdd]').val();
			movie.description = $('[name=descriptionAdd]').val();
			$.ajax({
				type : 'POST',
				url : '/cine-mtl/adminAdd',
				data : "movie=" + JSON.stringify(movie),

				success : function(data) {
					$("#add").modal("toggle");
					location.reload();
				},
				error : function(data) {
					console.log('not logout');
				}
			});
			
		});

});